﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StatusController.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   Defines the StatusController type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.MVC.Controllers
{
    using System.Linq;
    using System.Web.Mvc;
    using Core.Models;
    using Helpers;

    /// <summary>
    /// Defines the StatusController type.
    /// </summary>
    public class StatusController : BaseController
    {
        /// <summary>
        /// Gets the list of statuses of the associated customer
        /// GET: /Admin/Status/
        /// </summary>
        /// <returns>Index view</returns>
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult Index()
        {
            return View(Entities.Status.Where(s => s.StatusCustomerID == Customer.CustomerID));
        }

        /// <summary>
        /// Gets the category associated with the specified id
        /// GET: /Admin/Status/Details/5
        /// </summary>
        /// <param name="id">The status identificator</param>
        /// <returns>Details view</returns>
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult Details(int id)
        {
            var element = Entities.Status.FirstOrDefault(s => s.StatusCustomerID == Customer.CustomerID && s.StatusID == id);

            if (element == null)
            {
                // TODO: Handle exception
                ViewData.Add("Message", "The specified element does not exist of you do not have privileges to access it.");
                return View("Error");
            }

            return View(element);
        }

        /// <summary>
        /// Gets an empty status so the user is able to fill the needed information to create a new one.
        /// GET: /Admin/Status/Create
        /// </summary>
        /// <returns>Create view</returns>
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult Create()
        {
            var entity = new Status { StatusCustomerID = Customer.CustomerID };
            return View(entity);
        }

        /// <summary>
        /// Creates a new status with the information in the form.
        /// POST: /Admin/Status/Create
        /// </summary>
        /// <param name="collection">The form fields collection</param>
        /// <returns>Create view</returns>
        [HttpPost]
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult Create(FormCollection collection)
        {
            var entity = new Status();
            try
            {
                UpdateModel(entity);
                entity.Customer = Customer;
                entity.StatusImageUrl = "/images/default/pushpingeneric.png";
                Entities.AddObject("Status", entity);
                Entities.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(entity);
            }
        }

        /// <summary>
        /// Gets a status in edit mode so the user can change any field value.
        /// GET: /Admin/Status/Edit/5
        /// </summary>
        /// <param name="id">The status identificator</param>
        /// <returns>Edit mode view</returns>
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult Edit(int id)
        {
            var entity = Entities.Status.FirstOrDefault(s => s.StatusID == id);
            return View(entity);
        }

        /// <summary>
        /// Edits the existing status with the given identificator.
        /// POST: /Admin/Status/Edit/5
        /// </summary>
        /// <param name="id">The status identificator</param>
        /// <param name="collection">The form fields collection.</param>
        /// <returns>Edit mode view</returns>
        [HttpPost]
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var entity = Entities.Status.FirstOrDefault(s => s.StatusID == id);

            try
            {
                UpdateModel(entity);
                Entities.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(entity);
            }
        }

        /// <summary>
        /// Gets a status so the user can select to delete it.
        /// GET: /Admin/Status/Delete/5
        /// </summary>
        /// <param name="id">The status identificator</param>
        /// <returns>Delete confirmation view</returns>
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult Delete(int id)
        {
            return View(Entities.Status.FirstOrDefault(s => s.StatusID == id));
        }

        /// <summary>
        /// Deletes the specified id.
        /// POST: /Admin/Status/Delete/5
        /// </summary>
        /// <param name="id">The status id</param>
        /// <param name="collection">The form value collection</param>
        /// <returns>Index view</returns>
        [HttpPost]
        [StreetCareAuthorize(Roles = "admin")]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var entity = Entities.Status.FirstOrDefault(s => s.StatusID == id);
                Entities.DeleteObject(entity);
                Entities.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View("Error");
            }
        }
    }
}
